#include <iostream>
#include <set>
#include <map>
#include <string>
#include <algorithm>
using namespace std;
const int maxn = 30005;
bool cmp(pair<string,int>&a, pair<string,int>&b){
    return a.second > b.second;
}
pair<string, int> mp[maxn];
int main() {
    int n,q;
    string s;
    string name;
    int mi;
    set<string> myset;
    string key;

    cin>>n>>q;
    for(int i = 0; i < q; i++){
        cin>>s;
        myset.insert(s);
    }
    for(int i = 0; i < n; i++){
        cin>>name>>mi;
        int count = 0;
        for(int j = 0; j < mi; j++){
            cin>>key;
            auto it = myset.find(key);
            if (it != myset.end()) count++;
        }
        mp[i].first = name;
        mp[i].second = count;
    }
    //sort(mp, mp+n, cmp);
    sort(mp, mp+n, [](pair<string,int>&a, pair<string,int>&b){
        return a.second > b.second;
    });
    for(int i = 0; i < n; i++){
        cout<<mp[i].first<<endl;
    }
}
// 64 位输出请用 printf("%lld")